package com.emotionalEnvelope.edu.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emotionalEnvelope.edu.core.RestResponse;
import com.emotionalEnvelope.edu.dto.MyMsgDto;
import com.emotionalEnvelope.edu.mapper.EmailMapper;
import com.emotionalEnvelope.edu.model.Email;
import com.emotionalEnvelope.edu.service.IEmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author lqq
 * @Date 2023/05/09 23:36
 */
@Service
@Slf4j
public class EmailService extends ServiceImpl<EmailMapper, Email> implements IEmailService {

    @Resource
    private EmailMapper emailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse<String> toSend(Email param) {
        Email email = new Email();
        if (param.getType().equals(1) && param.getAcceptId() == null) {
            return RestResponse.failed("发送邮箱时接收人必填");
        } else if (param.getType().equals(1) && param.getAcceptId() != null) {
            //TODO 发送邮箱
            email.setType(1);
        } else {
            BeanUtils.copyProperties(param, email);
            param.setStatus(0);
            email.setCreateTime(new Date());
            emailMapper.insert(email);
        }
        return RestResponse.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse<String> reply(Email messages) {
        boolean empty = ObjectUtil.isEmpty(messages.getAcceptId());
        if (empty) {
            return RestResponse.failed("接收人不能为空");
        }
        messages.setCreateTime(new Date());
        messages.setStatus(0);
        messages.insert();
        return RestResponse.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse<String> read(Long id) {
        Email email = emailMapper.selectById(id);
        if (email == null) {
            return RestResponse.ok();
        }
        email.setStatus(1);
        super.saveOrUpdate(email);
        return RestResponse.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse<String> allRead(Long userId) {
        LambdaQueryWrapper<Email> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Email::getAcceptId, userId);
        List<Email> emails = emailMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(emails)) {
            return RestResponse.ok();
        }
        emails.forEach(v -> v.setStatus(1));
        saveOrUpdateBatch(emails);
        return RestResponse.ok();
    }

    @Override
    public RestResponse<List<MyMsgDto>> showMyMessage(Long id) {
        List<MyMsgDto> dto = emailMapper.showMyMsg(id);
        return RestResponse.ok(dto);
    }
}
